package com.nondo.rdp.finance.endpoint.api.app.course;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.rdp.auth.endpoint.model.ConvertCall;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.auth.endpoint.model.Query;
import com.nondo.rdp.core.common.BeanUtils;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.course.CourseClassify;
import com.nondo.rdp.finance.model.circle.CourseClassifyQuery;
import com.nondo.rdp.finance.service.course.CourseClassifyService;

@RestController("apiAppCourseClassify")
@RequestMapping("/api/app/courseClassify")
public class CourseClassifyEndpoint {
    @Autowired
    CourseClassifyService courseClassifyService;
    
    /**
     * 查看分类
     * @param request
     * @param response
     * @param id
     * @return
     */
    @RequestMapping("/info/{id}")
	public View<CourseClassifyQuery> info(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("id") Long id) {
		View<CourseClassifyQuery> view = new View<CourseClassifyQuery>(20000, "获取数据成功");
		
		CourseClassifyQuery courseClassifyQuery = new CourseClassifyQuery();
		if (id != null) {
			CourseClassify courseClassify = courseClassifyService.findOne(id);
			BeanUtils.copy(courseClassify, courseClassifyQuery);
			List<CourseClassify> subClassifyList = courseClassifyService.findByPid(courseClassify.getId());
			courseClassifyQuery.setSubClassifyList(subClassifyList);
		} 
		view.setData(courseClassifyQuery);
		return view;
	}
    
    /**
     * 获取分类列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public View<Page<CourseClassifyQuery>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<CourseClassify> query) {
        View<Page<CourseClassifyQuery>> view = new View<Page<CourseClassifyQuery>>(20000, "获取列表成功");

        CourseClassify courseClassify = query.getData();
        if(courseClassify == null){
        	courseClassify = new CourseClassify();
        }
        
        courseClassify.setPid(0L);

        Page<CourseClassify> page = courseClassifyService.findAll(new Page<CourseClassify>(query.getPage(), query.getSize()), courseClassify);
        
        Page<CourseClassifyQuery> resultPage = page.convertPage(new ConvertCall<CourseClassify, Page<CourseClassifyQuery>>() {

			@Override
			public Page<CourseClassifyQuery> execute(Page<CourseClassify> srcPage) {
				Page<CourseClassifyQuery> courseClassifyPage = new Page<CourseClassifyQuery>(srcPage.getPageNumber(),srcPage.getPageSize(),srcPage.getTotalPages(),srcPage.getTotalElements());
				List<CourseClassifyQuery> courseClassifyQueryList = new ArrayList<CourseClassifyQuery>();
				List<CourseClassify> courseClassifyList = srcPage.getContent();
				for(CourseClassify courseClassify : courseClassifyList) {
					CourseClassifyQuery courseClassifyQuery = new CourseClassifyQuery();
					BeanUtils.copy(courseClassify, courseClassifyQuery);
					List<CourseClassify> subClassifyList = courseClassifyService.findByPid(courseClassify.getId());
					courseClassifyQuery.setSubClassifyList(subClassifyList);
					courseClassifyQueryList.add(courseClassifyQuery);
				}
				courseClassifyPage.setContent(courseClassifyQueryList);
				return courseClassifyPage;
			}
        	
		});

        view.setData(resultPage);

        return view;
    }

}